<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable, ChangeDateFormat;

    protected $dates = [
        'created_at',
        'updated_at',
        'last_logined_at',
        'reg_at',
    ];
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected     $fillable  = [
        'mobile',
        'password',
        'enable',
        'sex',
        'name',
        'english_name',
        'nickname',
        'tel',
        'email',
        'avatar',
        'reg_ip',
        'reg_at',
        'last_logined_at',
        'last_logined_ip',
    ];
    public static $sexMap    = [
        'male'   => '男',
        'famale' => '女',
    ];
    public static $enableMap = [
        0 => '冻结',
        1 => '正常',
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
    ];

    /**
     * boot
     */
    public static function boot()
    {
        parent::boot();
        self::creating(function ($model) {
            $model->reg_ip = ip();
        });
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    public function getSexNameAttribute()
    {
        return self::$sexMap[$this->sex] ?? '';
    }
}
